<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="height: 3000px">
  <div id="app">
    <button @click="isShow = !isShow">{{isShow?'隐藏':'显示'}}</button>
    <home v-if="isShow"></home>
  </div>
  <script src="./vue.js"></script>
  <script>
    
    const Home = {
      template: `
        <div>
          <h2>这是一个组件</h2>  
        </div>
      `,
      data(){
        return {
          timer: null
        }
      }, 
      mounted(){
        this.timer = setInterval(() => {
         console.log(11);
       }, 1000);
       window.onscroll = function(){
         var sTop = document.documentElement.scrollTop || document.body.scrollTop;
         console.log(sTop);
       }
      }, 
      beforeDestroy(){
        console.log('组件卸载之前');
        clearInterval(this.timer);
        window.onscroll = null;
      },
      destroyed(){
        console.log('组件已经卸载');
      }
    }

    Vue.component('Home', Home)
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })
  </script>
</body>
</html>